import { Component, OnInit, ViewChild } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFUISchema } from '@delon/form';
import { environment } from '@env/environment';

@Component({
  selector: 'app-sys-file-upload',
  templateUrl: './upload.component.html',
})
export class OssFileUploadComponent implements OnInit {
  record: any = {};
  i: any;
  schema: SFSchema = {
    properties: {
      batchId: { type: 'string', title: '批次ID' },
      file: { type: 'string', title: '文件' },
    },
    required: [],
  };
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 12 },
    },
    $file: {
      widget: 'upload',
      action: `${environment.oss.ctx}/ossFile/upload`,
      multiple: true,
      data: () => this.i,
    },
  };

  constructor(private modal: NzModalRef, private msgSrv: NzMessageService, public http: _HttpClient) {}

  ngOnInit(): void {
    if (this.record.id > 0)
      this.http.get(`${environment.oss.ctx}/OssFile/${this.record.id}`).subscribe(res => (this.i = res));
  }

  save(value: any) {
    this.http.put(`${environment.oss.ctx}/OssFile`, value).subscribe(res => {
      this.msgSrv.success('保存成功');
      this.modal.close(true);
    });
  }

  saveAs(value: any) {
    value.id = null;
    this.save(value);
  }

  close() {
    this.modal.destroy();
  }
}
